package com.example.test;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class MainActivity extends Activity{
	private FragmentManager fragmentManager;//FragMent管理者
	private FragmentTransaction fragmentTransaction;//FragMent返回的接收器
	private ShakeListener mShakeListener;//摇晃监听器
	@Override
	public void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState); 
	setContentView(R.layout.activity_main);
	mShakeListener = new ShakeListener(MainActivity.this);//构造一个摇晃监听器
	mShakeListener.setOnShakeListener(new com.example.test.ShakeListener.OnShakeListener() {
	@Override
	public void onShake() {
		// TODO Auto-generated method stub
		mShakeListener.stop();//停止检测
		new Handler().postDelayed(new Runnable() {
			
			@SuppressLint("ShowToast")
			@Override
			public void run() {
				// TODO Auto-generated method stub
				fragmentManager = getFragmentManager();//获得一个FragMent管理者
				fragmentTransaction = fragmentManager.beginTransaction();//开始FragMent
		        MyFragment myfragment = new MyFragment();//创建获得MyFragMent对象
		        fragmentTransaction.add(R.id.linear, myfragment);//添加FragMent注意 第一个参数是id型的
		        fragmentTransaction.commit();//提交FragMent
		        mShakeListener.start();//摇晃重新监听
			}
		}, 2000);//间隔2秒开始检测摇晃，显示FragMent内容
		}
	});
	}
	@Override
	protected void onDestroy() {
	  super.onDestroy();
	  if (mShakeListener != null) {
	   mShakeListener.stop();//停止检测
	  }
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
